﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using condortravel.operaciones.core.DataModel;
using System.Data.Linq;

namespace condortravel.operaciones.persistence.operacionesImpl
{
    public class PaqueteProgramadoDAOImpl : condortravel.operaciones.persistence.Impl.GenericDAO<Tb_PaqueteProgramado, int>
    {
        public override Tb_PaqueteProgramado update(Tb_PaqueteProgramado entity)
        {
            Tb_PaqueteProgramado evento;
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }

                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    evento = dataContext.GetTable<Tb_PaqueteProgramado>().Where(p => p.paqueteTurProg_ID == entity.paqueteTurProg_ID).Single();
                    evento.estadoPaquete_ID = entity.estadoPaquete_ID;
                    evento.fechaFin = entity.fechaFin;
                    evento.fechaInicio = entity.fechaInicio;
                    evento.nroTuristas = entity.nroTuristas;
                    evento.paqueteTuristico_ID = entity.paqueteTuristico_ID;
                    dataContext.SubmitChanges();
                }
            }
            return evento;
            


        }

        public override Tb_PaqueteProgramado getByID(int id)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    DataLoadOptions options = new DataLoadOptions();
                    options.LoadWith<Tb_PaqueteProgramado>(s => s.Tb_PaqueteTuristico);
                    
                    dataContext.LoadOptions = options;
                    return (from p in dataContext.GetTable<Tb_PaqueteProgramado>()
                            where p.paqueteTurProg_ID== id
                            select p).SingleOrDefault<Tb_PaqueteProgramado>();

                }
            }
        }

        public override void delete(int pk)
        {
            throw new NotImplementedException();
        }
        public IList<Tb_PaqueteProgramado> getListPaqueteProgramadoPendientes()
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    DataLoadOptions options = new DataLoadOptions();
                    options.LoadWith<Tb_PaqueteProgramado>(s => s.Tb_PaqueteTuristico);

                    dataContext.LoadOptions = options;

                    return (from p in dataContext.GetTable<Tb_PaqueteProgramado>()
                            where
                            (p.estadoPaquete_ID.ToString().Trim().Equals("100") )
                            select p).ToList<Tb_PaqueteProgramado>();
                }

            }
        }
    }
}
